﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace HRDexCapstoneProject.Functions.Forms
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        DataTable dtbl = new DataTable("dtbl");
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmd22 = new SqlCommand("SELECT EmployeeID, LastName + ', ' + FirstName + ' ' + MiddleName AS Name, DeptID, catPositionsID, CollegeID FROM formEmployees", conn);
            SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
            DataTable dt22 = new DataTable("dt22");

            da22.Fill(dt22);

            txtName.Text = dt22.Rows[0].ItemArray[1].ToString();
            ddlPos.SelectedValue = dt22.Rows[0].ItemArray[3].ToString();
            ddlDept.SelectedValue = dt22.Rows[0].ItemArray[4].ToString();
            txtExplanation.Visible = false;
            Label1.Visible = false;
            ddlApproval.Visible = false;
            Selected();
        }
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string NoticeOfAbsenceID = Request.QueryString["NoticeOfAbsenceID"];

            if (txtNo.Text != NoticeOfAbsenceID)
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO formNoticeofAbsence(EmpName, PosID, DeptID, ForEmpID, DateTime, Reason, pubDate) " +
                "VALUES (@empName, @pos,@Dept,@forEmp,@dt,@Reason,@pubDate)", conn);
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                conn.Open();
     
                cmd.Parameters.Add("@empName", SqlDbType.VarChar).Value = txtName.Text;
                cmd.Parameters.Add("@pos", SqlDbType.VarChar).Value = ddlPos.SelectedValue;
                cmd.Parameters.Add("@Dept", SqlDbType.VarChar).Value = ddlDept.SelectedValue;
                cmd.Parameters.Add("@forEmp", SqlDbType.VarChar).Value = ddlToEmp.SelectedValue;
                cmd.Parameters.Add("@dt", SqlDbType.VarChar).Value = dpicker.SelectedDateString      + " " + ddlFHour.SelectedValue + ":" + ddlFMin.SelectedValue + " - " + ddlTHour.SelectedValue + ":" + ddlTmin.SelectedValue;
                cmd.Parameters.Add("@Reason", SqlDbType.VarChar).Value = txtReason.Text;
                cmd.Parameters.Add("@pubDate", SqlDbType.VarChar).Value = DateTime.Now;

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            else
            {
                Edit();
            }
        }


        public void Selected()
        {
            string NoticeOfAbsenceID = Request.QueryString["NoticeOfAbsenceID"];

            if (NoticeOfAbsenceID != null)
            {
                SqlCommand cmd33 = new SqlCommand("SELECT EmpName, PosID, DeptID, ForEmpID, DateTime, Reason, pubDate, ApprovalFac, NoticeOfAbsenceID FROM formNoticeofAbsence WHERE (NoticeOfAbsenceID =" + NoticeOfAbsenceID + ")", conn);
                SqlDataAdapter da33 = new SqlDataAdapter(cmd33);
                DataTable dt33 = new DataTable("dt33");
                da33.Fill(dt33);
                txtDate.Enabled = false;
                txtName.Enabled = false;
                ddlDept.Enabled = false;
                ddlFHour.Visible = false;
                ddlFMin.Visible = false;
                ddlPos.Enabled = false;
                ddlTHour.Visible = false;
                ddlTmin.Visible = false;
                ddlToEmp.Enabled = false;
                dpicker.Visible = false;
                txtReason.Enabled = false;
                txtExplanation.Visible = true;
                Label1.Visible = true;
                ddlApproval.Visible = false;
                txtNo.Text = dt33.Rows[0].ItemArray[8].ToString();
                txtName.Text = dt33.Rows[0].ItemArray[0].ToString();
                ddlDept.SelectedValue = dt33.Rows[0].ItemArray[2].ToString();
                ddlToEmp.SelectedValue = dt33.Rows[0].ItemArray[3].ToString();
                txtDate.Text = dt33.Rows[0].ItemArray[4].ToString();
                txtReason.Text = dt33.Rows[0].ItemArray[5].ToString();
                lblpubDate.Text = dt33.Rows[0].ItemArray[6].ToString();
  
                    if (dt33.Rows[0].ItemArray[7].ToString() == "0" || dt33.Rows[0].ItemArray[7].ToString() == "1")
                    {
                        ddlApproval.SelectedValue = dt33.Rows[0].ItemArray[7].ToString();
                        ddlApproval.Enabled = false;
                        txtExplanation.Enabled = false;
                    }
                    else
                    {
                        ddlApproval.Enabled = true;
                        txtExplanation.Enabled = true;
                        Label1.Enabled = true;
                       
                    }

                    SqlCommand cmd123 = new SqlCommand("SELECT formEmployees.EmployeeID, catUserLevel.catUserLevelName, " +
                        "catPositions.catPositionsName, catDepartment.DepartmentName FROM formEmployees INNER " +
                        "JOIN catUserLevel ON formEmployees.UserLevelID = catUserLevel.UserLevelID INNER JOIN " +
                        "catPositions ON formEmployees.catPositionsID = catPositions.catPositionsID INNER " +
                        "JOIN catDepartment ON formEmployees.DeptID = catDepartment.DeptID WHERE (formEmployees.EmployeeID=" + SessionID() + ")", conn);
                    SqlDataAdapter da123 = new SqlDataAdapter(cmd123);
                    DataTable dt123 = new DataTable("dt123");
                    da123.Fill(dt123);

                    if (dt123.Rows[0].ItemArray[2].ToString() == "Faculty")
                    {
                        ddlApproval.Visible = true;
                        ddlApproval.Enabled = true;
                    }
                    else
                    {
                        ddlApproval.Enabled = false;
                        ddlApproval.Visible = true;
                    }
            }

        }
        public void Edit()
        {
            string NoticeOfAbsenceID = Request.QueryString["NoticeOfAbsenceID"];
            SqlCommand cmd123 = new SqlCommand("UPDATE formNoticeofAbsence SET ApprovalFac = @approval, remarksSend =@rSend, pubDateReturn =@dateReturn WHERE (NoticeOfAbsenceID=" + NoticeOfAbsenceID+")", conn);
            SqlDataAdapter da123 = new SqlDataAdapter(cmd123);
         
            conn.Open();
            cmd123.Parameters.AddWithValue("@approval", SqlDbType.Int).Value = ddlApproval.SelectedValue;
            cmd123.Parameters.AddWithValue("@rSend",SqlDbType.VarChar).Value = txtExplanation.Text;
            cmd123.Parameters.AddWithValue("@dateReturn", SqlDbType.VarChar).Value = DateTime.Now;
            cmd123.ExecuteNonQuery();
            conn.Close();
        }
    }
}